package io.jt808.proto.cmd;

import io.norland.buffer.AutoBuffer;
import io.norland.annotations.Proto;
import io.norland.proto.IProto;
import io.jt808.util.Tools;
import lombok.Data;

import java.util.Date;

/**
 * 存储多媒体数据上传命令
 */
@Proto("8803")
@Data
public class JT8803 implements IProto {
    /**
     * 多媒体类型
     */
    private byte multimediaType;
    /**
     * 通道ID
     */
    private byte channelId;
    /**
     * 事件项编码
     */
    private byte eventCode;
    /**
     * 起始时间
     */
    private Date startTime;
    /**
     * 结束时间
     */
    private Date endTime;

    /**
     * 删除标志
     */
    private byte delete;

    public final byte[] writeToBytes() {
        AutoBuffer buffer = new AutoBuffer();
        buffer.writeByte(multimediaType);
        buffer.writeByte(channelId);
        buffer.writeByte(eventCode);
        buffer.writeBcdString(Tools.getBcdDateTime(startTime));
        buffer.writeBcdString(Tools.getBcdDateTime(endTime));
        buffer.writeByte(delete);
        return buffer.array();
    }

    public final void readFromBuf(AutoBuffer buffer) {
        multimediaType = buffer.readByte();
        channelId = buffer.readByte();
        eventCode = buffer.readByte();
        startTime = Tools.getDateTime(buffer.readBcd(), buffer.readBcd(), buffer.readBcd(),
                buffer.readBcd(), buffer.readBcd(), buffer.readBcd());
        endTime = Tools.getDateTime(buffer.readBcd(), buffer.readBcd(), buffer.readBcd(),
                buffer.readBcd(), buffer.readBcd(), buffer.readBcd());
        delete = buffer.readByte();
    }

//    public static void main(String[] args) {
//        byte[] bytes = new byte[]{
//                0x01,
//                0x0F,
//                0x01,
//                0x18, 0x10, 0x26, 0x17, 0x15, 0x13,
//                0x18, 0x10, 0x30, 0x17, 0x15, 0x13,
//                0x00
//        };
//        JT8803 jt8600 = new JT8803();
//        jt8600.readFromBuf(new AutoBuffer(bytes));
//        System.out.println(new String(bytes).equals(new String(jt8600.writeToBytes())));
//    }
}